"""Field

 [...]
"""
import os

from jinja2 import Environment, FileSystemLoader

from models import items

env = Environment(trim_blocks=True, lstrip_blocks=True, 
                  loader=FileSystemLoader('templates'))


def upperfirst(s):
    return s[:1].upper() + s[1:]

env.filters['upperfirst'] = upperfirst

template = env.get_template('processor.html')


def codegen():
    for item in items:
        title = item['title']
        classname = item['classname']
        methods = item['methods']
    
        code = template.render(title=title,
                               methods=methods,
                               classname=classname,)
        #print('\n'.join(codes))
    
        with open('gen/processor-impl/{}.py'.format(classname.lower()), 'w') as f:
            f.write(code)
        #break


def postsed():
    a = [
        ('gen/processor-impl/member.py', 'MemberUserIDDuplicatedError', 'ClubMemberDuplicatedError'),
    ]
    for x in a:
        command = "sed -i 's/{}/{}/g' {}".format(x[1], x[2], x[0])
        os.system(command)


if __name__ == '__main__':
    codegen()
    postsed()
